// unpack array to another array
let array1 = [1, 2, 3];
let array2 = [0, ...array1, 4];
console.log(array2);

// unpack object to another object
let object1 = { a: 1, b: 2 };
let object2 = { z: 0, ...object1, c: 3 };
console.log(object2);

// unpack string to an array
const array = [...'hello'];
console.log(array);

// unpack function actual parameter
function f(a, b) {
	return a + b;
}
let pair = [1, 2];
console.log(f(...pair));

// pack function formal parameter
function f1(arg, ...args) {
	console.log(arg);
	console.log(args);
}
f1(1, 2, 3, 4);

// unpack array
let [a, b, ...others] = array1;
console.log(a, others, b);

// unpack object
({ a, b, ...rest } = object2);
console.log(a, b, rest);

// unpack function parameter
function f2(...[a, b, c]) {
	return a + b + c;
}
console.log(f2(1, 2, 3, 4));

// assign through array
[a, b] = [10, 20];
console.log(a, b);
[a, [b, [c]]] = [1, [2, [3]]];
console.log(a, b, c);
[a = 3, b = 9] = [1];
console.log(a, b);
[a, b = 1] = [10, undefined];
console.log(a, b);
[a = 1] = [null];
console.log(a);
a = 1, b = 2;
console.log(a, b);
[a, b] = [b, a];
console.log(a, b);
[a, , , b] = [10, 20, 30, 40];
console.log(a, b);
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(a, b, rest);

// assign through object
let object = { name: 'imooc', age: 7 };
let { name, age } = object;
console.log(name, age);
({ a=10, b=5 } = { a: 3 });
console.log(a, b);
({ a=10, b=5 } = { a: 3, b: undefined });
console.log(a, b);
({ a=10, b=5 } = { a: 3, b: null });
console.log(a, b);
({ a, b: { c }, d: [e] } = { a: 3, b: { c: 4 }, d: [6] });
console.log(a, c, e);
// rename
let { a: x = 8, b: y = 3 } = { a: 2 }
console.log(x, y);

// assign through string
[a, b, c, d, e] = 'imooc';
console.log(a, b, c, d, e);
let { length: len } = 'hello';
console.log(len);
